UIWindow *_mainWindow() {
    
    id appDelegate = [UIApplication sharedApplication].delegate;
    
    if (appDelegate && [appDelegate respondsToSelector:@selector(window)]) {
        return [appDelegate window];
    }
    
    NSArray *windowsArr = [UIApplication sharedApplication].windows;
    
    if ([windowsArr count] == 1) {
        
        return [windowsArr firstObject];
    } else {
        
        for (UIWindow *window in windowsArr) {
            if (window.windowLevel == UIWindowLevelNormal) {
                return window;
            }
        }
    }
    return nil;
}

#import "SXLoadingView.h"
#import "MBProgressHUD.h"

static MBProgressHUD  *s_progressHUD = nil;

@implementation SXLoadingView


+ (void)showProgressHUD:(NSString *)aString duration:(CGFloat)duration {
    [self hideProgressHUD];
    MBProgressHUD *progressHUD = [[MBProgressHUD alloc] initWithView:_mainWindow()];
    [_mainWindow() addSubview:progressHUD];
    progressHUD.animationType = MBProgressHUDAnimationZoom;
    progressHUD.label.text = aString;
    
    progressHUD.removeFromSuperViewOnHide = YES;
//    progressHUD.opacity = 0.7;

    [progressHUD showAnimated:YES];
    [progressHUD hideAnimated:YES afterDelay:duration];
}

+ (void)showProgressHUD:(NSString *)aString {
    if (!s_progressHUD) {
        static dispatch_once_t once;
        dispatch_once(&once, ^{
            s_progressHUD = [[MBProgressHUD alloc] initWithView:_mainWindow()];
        });
    } else {
        [s_progressHUD hideAnimated:YES];
    }
    [_mainWindow() addSubview:s_progressHUD];
    s_progressHUD.removeFromSuperViewOnHide = YES;
    s_progressHUD.animationType = MBProgressHUDAnimationZoom;
    if ([aString length] > 0) {
      s_progressHUD.label.text = aString;
    }
    else s_progressHUD.label.text = nil;
    
//    s_progressHUD.opacity = 0.7;
    [s_progressHUD showAnimated:YES];
}

+ (void)showAlertHUD:(NSString *)aString duration:(CGFloat)duration {
    [self hideProgressHUD];
    MBProgressHUD *progressHUD = [[MBProgressHUD alloc] initWithView:_mainWindow()];
    [_mainWindow() addSubview:progressHUD];
    progressHUD.animationType = MBProgressHUDAnimationZoom;
    progressHUD.label.text =aString;
    progressHUD.label.numberOfLines = 0;
    progressHUD.removeFromSuperViewOnHide = YES;
//    progressHUD.opacity = 0.7;
    progressHUD.mode = MBProgressHUDModeText;
    [progressHUD showAnimated:YES];
    [progressHUD hideAnimated:YES afterDelay:duration];
}

+ (void)hideProgressHUD {
    if (s_progressHUD) {
        [s_progressHUD hideAnimated:YES];
    }
}

+ (void)updateProgressHUD:(NSString *)progress {
    if (s_progressHUD) {
        s_progressHUD.label.text = progress;
    }
}


@end
